package com.zx.controller;

import com.zx.bean.Message;
import com.zx.bean.Performance;
import com.zx.bean.PerformanceResult;
import com.zx.mvc.ResponseBody;
import com.zx.service.PerformanceResultService;
import com.zx.service.PerformanceService;
import com.zx.util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;

public class PerformanceController {
    PerformanceService service=new PerformanceService();
    PerformanceResultService service2=new PerformanceResultService();
    @ResponseBody("/performance/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
         double tatalArea= Double.valueOf(request.getParameter("tatalArea"));
        double tatalBuilUpArea=Double.valueOf(request.getParameter("tatalBuilUpArea"));
        double landArea=Double.valueOf(request.getParameter("landArea"));
        int qualifiedProductQuantity= Integer.parseInt(request.getParameter("qualifiedProductQuantity"));
        double outputOfMajorPollutans=Double.valueOf(request.getParameter("outputOfMajorPollutans"));
        double exhaustGasProduction=Double.valueOf(request.getParameter("exhaustGasProduction"));
        double wasteWaterProduction=Double.valueOf(request.getParameter("wasteWaterProduction"));
        double consumptionOfMajorRawMaterials=Double.valueOf(request.getParameter("consumptionOfMajorRawMaterials"));
        //double comprehensiveUtilizationOfIndustrialSolidWaste=Double.valueOf(request.getParameter("comprehensiveUtilizationOfIndustrialSolidWaste"));
        double productionOfIndestrialSolid=Double.valueOf(request.getParameter("productionOfIndestrialSolid"));
        double wasteWaterTreatmentRecyclingAmount=Double.valueOf(request.getParameter("wasteWaterTreatmentRecyclingAmount"));
        double amountOfPhysicalEnergyConsumed=Double.valueOf(request.getParameter("amountOfPhysicalEnergyConsumed"));
        double carbonDioxideEmissions=Double.valueOf(request.getParameter("carbonDioxideEmissions"));
        double zt=Double.valueOf(request.getParameter("zt"));
        double zw=Double.valueOf(request.getParameter("zw"));
        double vd=Double.valueOf(request.getParameter("vd"));

        String year=request.getParameter("carbonDioxideEmissions");
        String mounth=request.getParameter("mounth");
       // String companyNo=request.getParameter("companyNo");

        Performance performance=new Performance();
        performance.setTatalArea(tatalArea);
        performance.setTatalBuilUpArea(tatalBuilUpArea);
        performance.setLandArea(landArea);
        performance.setQualifiedProductQuantity(qualifiedProductQuantity);
        performance.setOutputOfMajorPollutans(outputOfMajorPollutans);
        performance.setExhaustGasProduction(exhaustGasProduction);
        performance.setWasteWaterProduction(wasteWaterProduction);
      //  performance.setConsumptionOfMajorRawMaterials(consumptionOfMajorRawMaterials);
        //performance.setComprehensiveUtilizationOfIndustrialSolidWaste(comprehensiveUtilizationOfIndustrialSolidWaste);
        performance.setProductionOfIndestrialSolid(productionOfIndestrialSolid);
        performance.setWasteWaterTreatmentRecyclingAmount(wasteWaterTreatmentRecyclingAmount);
        performance.setAmountOfPhysicalEnergyConsumed(amountOfPhysicalEnergyConsumed);
        performance.setCarbonDioxideEmissions(carbonDioxideEmissions);
        performance.setYear(year);
        performance.setMounth(mounth);
      //  performance.setCompanyNo(companyNo);
        boolean result= service.insert(performance);

        //计算各种指标数据
        DecimalFormat df1=new DecimalFormat("0.000");


        PerformanceResult performanceResult=new PerformanceResult();
        //容积率
         double plotRatio= Double.parseDouble(df1.format((tatalArea+tatalBuilUpArea)/landArea));
       // System.out.println(df1.format((tatalArea+tatalBuilUpArea)/landArea)+" "+plotRatio);
        //单位产品污染量
         double pollutionProductionPerUnitProduct= Double.parseDouble(df1.format(outputOfMajorPollutans/qualifiedProductQuantity));
        //System.out.println(df1.format(outputOfMajorPollutans/qualifiedProductQuantity)+" "+pollutionProductionPerUnitProduct);
        //每单位产品的废气产量
         double exhaustGasProductionPerUnitProduct= Double.parseDouble(df1.format(exhaustGasProduction/qualifiedProductQuantity));
        //单位产品产生的废水量
          double amountOfWasteWaterProducedPerUnitProduct= Double.parseDouble(df1.format(wasteWaterProduction/qualifiedProductQuantity));
        //每单位产品的原材料消耗
         double consumptionOfRawMaterialsPerUnitOfProduct= Double.parseDouble(df1.format(consumptionOfMajorRawMaterials/qualifiedProductQuantity));
        //COMPERHENSIVE_UTILIZATION_RATE_OF_INDUSTRIAL_SOLID_WASTE工业固体废物综合利用率
         double comperhensiveUtilizationRateOfIndustrialSolidWaste= Double.parseDouble(df1.format(zt/(zw+productionOfIndestrialSolid)));
        //WASTE_WATER_RECYCLING_RATE 废水回收率
         double wasteWaterRecyclingRate= Double.parseDouble(df1.format(wasteWaterTreatmentRecyclingAmount/(wasteWaterTreatmentRecyclingAmount+vd)));
        //COMPREHENSIVE_ENERGY_CONSUMPTION_PER_UNIT_PRODUCT单位产品综合能耗
         double comprehensiveEnergyConsumptionPerUnitProduct= Double.parseDouble(df1.format(amountOfPhysicalEnergyConsumed/qualifiedProductQuantity));
        //CARBON_EMISSION_PER_UNIT_PRODUC 单位产品碳排放量
         double carbonEmissionPerUnitProduc= Double.parseDouble(df1.format(carbonDioxideEmissions/qualifiedProductQuantity));
        performanceResult.setPlotRatio(plotRatio);
        performanceResult.setPollutionProductionPerUnitProduct(pollutionProductionPerUnitProduct);
        performanceResult.setExhaustGasProductionPerUnitProduct(exhaustGasProductionPerUnitProduct);
        performanceResult.setAmountOfWasteWaterProducedPerUnitProduct(amountOfWasteWaterProducedPerUnitProduct);
        performanceResult.setConsumptionOfRawMaterialsPerUnitOfProduct(consumptionOfRawMaterialsPerUnitOfProduct);
        performanceResult.setComperhensiveUtilizationRateOfIndustrialSolidWaste(comperhensiveUtilizationRateOfIndustrialSolidWaste);
        performanceResult.setWasteWaterRecyclingRate(wasteWaterRecyclingRate);
        performanceResult.setComprehensiveEnergyConsumptionPerUnitProduct(comprehensiveEnergyConsumptionPerUnitProduct);
        performanceResult.setCarbonEmissionPerUnitProduc(carbonEmissionPerUnitProduc);
        performanceResult.setMounth(mounth);
        performanceResult.setYear(year);
        //performanceResult.setCompanyNo(companyNo);
        boolean result2=service2.insert(performanceResult);

        Message msg=null;
        if(result&&result2){
            msg=new Message(0,"保存并且分析成功");
        }else if(result){
            msg=new Message(-1,"表2插入失败");
        }else if(result2){
            msg=new Message(-1,"表1插入失败");
        }else {
            msg=new Message(-1,"两表插入失败");
        }
        String json= JsonUtil.toJson(msg);
        return json;
    }
}
